package com.example.crop_image_project;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * @author Elvis
 * @version v1.0
 * @since 2024/9/4.
 */
public class LoadingProgressDialog extends AlertDialog {
    protected TextView mTextViewNote;
    protected ProgressBar mProgressBar;

    public LoadingProgressDialog(Context context) {
        super(context);
    }

    public LoadingProgressDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init(getContext());
    }

    @SuppressLint("MissingInflatedId")
    private void init(Context context) {
        setContentView(R.layout.include_public_loading);
        mTextViewNote = (TextView) findViewById(R.id.tv_loading);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_loading);
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.WRAP_CONTENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        getWindow().setBackgroundDrawableResource(R.color.transparent);
        getWindow().setAttributes(params);
    }

    @Override
    public void setTitle(int titleId) {
        if (mTextViewNote != null) {
            mTextViewNote.setText(titleId);
        }
    }

    @Override
    public void setTitle(CharSequence title) {
        if (mTextViewNote != null) {
            mTextViewNote.setText(title);
        }
    }

    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    public static LoadingProgressDialog show(Context context, CharSequence title,
                                             CharSequence message) {
        return show(context, title, message, false);
    }

    public static LoadingProgressDialog show(Context context, CharSequence title,
                                             CharSequence message, boolean indeterminate) {
        return show(context, title, message, indeterminate, false, null);
    }

    public static LoadingProgressDialog show(Context context, CharSequence title,
                                             CharSequence message, boolean indeterminate, boolean cancelable) {
        return show(context, title, message, indeterminate, cancelable, null);
    }

    public static LoadingProgressDialog show(Context context, CharSequence title,
                                             CharSequence message, boolean indeterminate,
                                             boolean cancelable, OnCancelListener cancelListener) {
        LoadingProgressDialog dialog = new LoadingProgressDialog(context);
        dialog.setTitle(title);
        dialog.setMessage(message);
        dialog.setCancelable(cancelable);
        dialog.setOnCancelListener(cancelListener);
        dialog.show();
        return dialog;
    }

}
